package com.huadi.qi.controller;

import com.huadi.qi.bean.Means;
import com.huadi.qi.bean.News;
import com.huadi.qi.mapper.MeansMapper;
import com.huadi.qi.service.MeansService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
public class MeansController {
   @Autowired
    MeansMapper meansMapper;
   @Autowired
    MeansService meansService;

    //下载资料
    @GetMapping("/download")
    public String download(Model model){
        List<Means> list = meansService.list();
        model.addAttribute("mean",list);
        return "download";
    }
    //管理员端
    @GetMapping("/download/list")
    public String list(@RequestParam(defaultValue = "") String nation, Model model) {
        List<Means> list = new ArrayList<>();
        if (nation.equals("")) {
            list = meansService.list();
            model.addAttribute("download", list);
            return "/admin/download/list";
        } else {
            list = meansMapper.selectNation(nation);
            model.addAttribute("download", list);
            return "/admin/download/list";
        }
    }
    //删除
    @GetMapping("/download/del{id}")
    public String del(@PathVariable("id") Integer id, HttpSession session){
        boolean b = meansService.removeById(id);
        if (b == true){
            session.setAttribute("NewMsg","删除成功！");
            return "redirect:/download/list";
        }
            session.setAttribute("NewMsg","删除失败！");
            return "redirect:/download/list";

    }

    //添加
    @GetMapping("/download/add")
    public String add(){
        return "/admin/download/add";
    }
    @PostMapping("/download/add")
    public String postadd(Means means,HttpSession session) {
        try {
            meansMapper.insert(means);
            session.setAttribute("NewMsg", "添加成功");
            return "redirect:/download/list";
        } catch (Exception e) {
            session.setAttribute("NewMsg", "添加失败");
            return "redirect:/download/list";
        }
    }
    //修改
    @GetMapping("/download/update{id}")
    public String update(@PathVariable("id") Integer id,Model model){
        Means means = meansMapper.selectById(id);
        model.addAttribute("mean",means);
        return "/admin/download/update";
    }
    @PostMapping("/download/update")
    public String postupdate(Means means,HttpSession session) {
        try {
            meansMapper.updateById(means);
            session.setAttribute("NewMsg", "修改成功");
            return "redirect:/download/list";
        } catch (Exception e) {
            session.setAttribute("NewMsg", "修改失败");
            return "redirect:/download/list";
        }
    }
}
